package model;

import java.util.ArrayList;
import java.util.Date;

import model.type.SubjectAttributeType;
import model.type.SubjectType;
import db.ImbiConnection;

public class Person{	
		
	private final int id;
	private String firstName;
	private String lastName;
	private String identity;
	private Date birthday;
	private Integer createdBy;
	private Integer updatedBy;
	private Date created;
	private Date updated;
	
	public Person(int id, String firstName, String lastName, String identity,
			Date birthday, Integer createdBy, Integer updatedBy, Date created,
			Date updated) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.identity = identity;
		this.birthday = birthday;
		this.createdBy = createdBy;
		this.updatedBy = updatedBy;
		this.created = created;
		this.updated = updated;
	}

	public int getId() {
		return id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getIdentity() {
		return identity;
	}

	public void setIdentity(String identity) {
		this.identity = identity;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(int createdBy) {
		this.createdBy = createdBy;
	}

	public int getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(int updatedBy) {
		this.updatedBy = updatedBy;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}
	
	@Override
	public String toString() {		
		return getFirstName()+" "+getLastName();
	}
		
	/*OBJECT ACCESS METHODS*/
	
	public boolean isCustomer(){
		return getCustomer().size() > 0;
	}
	
	public ArrayList<Customer> getCustomer(){
		return ImbiConnection.getCustomerDAO().findByPersonId(this);
	}
	
	public ArrayList<Employee> getEmployee(){
		return ImbiConnection.getEmployeeDAO().findByPersonId(this);
	}
	
	public void makeCustomer(){
		Customer c = new Customer(0, getId(), SubjectType.PERSON);
		ImbiConnection.getCustomerDAO().insert(c);
	}
	
	public ArrayList<SubjectAttribute> getAttributes(){		
		ArrayList<SubjectAttribute> attrList = ImbiConnection.getSubjectAttributeDAO().findByPersonId(this);
		for(Customer c : getCustomer()){
			attrList.addAll(ImbiConnection.getSubjectAttributeDAO().findByCustomerId(c));
		}		
		for(Employee e : getEmployee()){
			attrList.addAll(ImbiConnection.getSubjectAttributeDAO().findByEmployeeId(e));
		}		
		return attrList;
	}
	
	public ArrayList<Address> getAddressList(){
		return ImbiConnection.getAddressDAO().findBySubjectId(getId(), SubjectType.PERSON);
	}
	
	public Employee getCreaterEmployee(){
		return getEmployeeById(getCreatedBy());
	}
	
	public ArrayList<Contact> getContactList(){
		return ImbiConnection.getContactDAO().getContacts(this);
	}
	
	public Employee getUpdaterEmployee(){
		return getEmployeeById(getUpdatedBy());
	}
	
	public ArrayList<EnterprisePersonRelation> getRelations(){
		return ImbiConnection.getEnterprisePersonRelationDAO().findAllByPerson(this);
	}	
	
	public ArrayList<SubjectAttributeType> getAvailableAttributes(){
		ArrayList<SubjectAttributeType> list = new ArrayList<SubjectAttributeType>();
		list.addAll(
			SubjectAttributeType.getAll(
				SubjectType.PERSON, 
				isCustomer()?SubjectType.CUSTOMER:0, 
				getEmployee().size()>0?SubjectType.EMPLOYEE:0
			)
		);		
		return list;
	}
	
	private Employee getEmployeeById(int id){
		return ImbiConnection.getEmployeeDAO().find(id);
	}	
	
	
}
